package com.dreamweaver.mvvmtest.view;

import android.opengl.ETC1;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.dreamweaver.mvvmtest.R;
import com.dreamweaver.mvvmtest.bean.LoginInfo;
import com.dreamweaver.mvvmtest.bean.LoginResponse;
import com.dreamweaver.mvvmtest.model.LoginModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends AppCompatActivity {
    @BindView(R.id.edt_username)
    EditText edt_username;
    @BindView(R.id.edt_password)
    EditText edt_password;
    LoginModel loginModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        loginModel = new ViewModelProvider(this).get(LoginModel.class);
        loginModel.getLoginResponseLiveData().observe(this, new Observer<LoginResponse>() {
            @Override
            public void onChanged(LoginResponse loginResponse) {
                if (loginResponse != null) {
                    Toast.makeText(LoginActivity.this, "登录成功，用户名是:"
                                    + loginResponse.getUserName() + ";token是:" + loginResponse.getToken(),
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @OnClick({R.id.btn})
    void click(View view) {
        if (edt_username.getEditableText().toString().isEmpty()) {
            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (edt_password.getEditableText().toString().isEmpty()) {
            Toast.makeText(this, "密码名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        loginModel.Login(new LoginInfo(edt_username.getEditableText().toString(),
                edt_password.getEditableText().toString()));
    }
}
